package com.cyq.controller;

import com.alibaba.fastjson.JSONArray;
import com.cyq.pojo.Bill;
import com.cyq.pojo.Login;
import com.cyq.pojo.Message;
import com.cyq.pojo.Product;
import com.cyq.service.BillService;
import com.cyq.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class shopController {
    int code=1;

    @Autowired
    private ShopService shopService;
    @Autowired
    private BillService billService;

    /*跳转到用户登录页面*/
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/shop/login";
    }

    @PostMapping("/login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mav = new ModelAndView();
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        Message message = new Message();
        message.setMsg("请检查输入是否正确");
        message.setCode("1");

        Login currentLogin = shopService.getPWDbyName(username);
        String passWorld = currentLogin.getPassWorld();
        if(pwd.equals(passWorld)){
            message.setCode("0");
            message.setMsg("登陆成功");
            message.setUsr_ID(currentLogin.getId());

        }
        try {
            response.setContentType("application/json;charset=utf-8");
            PrintWriter outPrintWriter = response.getWriter();
            outPrintWriter.write(JSONArray.toJSONString(message));
            outPrintWriter.flush();
            outPrintWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*跳转到用户登录页面*/
    @RequestMapping("/toIndex")
    public String toIndex(){
        return "/shop/indexLogin";
    }

    @RequestMapping("index")
    public ModelAndView index(@RequestParam("userId") int id){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/shop/index");
        return mav;
    }

    @RequestMapping("productInfoGet")
    public void getInfo(HttpServletResponse response){
        List<Product> productList = shopService.getProductList();
        Message message = new Message();
        message.setCode("0");
        message.setMsg("获取成功");
        message.setCount(1);
        message.setData(productList);
        try {
            response.setContentType("application/json;charset=utf-8");
            PrintWriter outPrintWriter = response.getWriter();
            outPrintWriter.write(JSONArray.toJSONString(message));
            outPrintWriter.flush();
            outPrintWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("buy")
    @ResponseBody
    public String buy(@RequestParam("productId") int productId,@RequestParam("userId") int userId){
        Product productById = shopService.getProductById(productId);
        Bill bill = new Bill();
        bill.setBillCode("BILL2022_"+code);
        System.out.println(productById.getProductName());
        bill.setProductName(productById.getProductName());
        bill.setProductDesc(productById.getProductDesc());
        bill.setProductUnit(productById.getProductUnit());
        bill.setProductCount(BigDecimal.valueOf(10));
        bill.setTotalPrice(BigDecimal.valueOf(10*(productById.getProductPrice())));
        bill.setIsPayment(1);
        Date date= new Date();
        bill.setCreationDate(date);
        bill.setProviderId(2);
        bill.setCreatedBy(1);
        int i = billService.billAdd(bill);
        if(i==1){
            code++;
            return "购买成功";
        }
        return "失败";
    }
}
